package com.johnsoft.pph;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class CabWriter {
	
	private DataOutputStream cab;
	
	public CabWriter() {
		if(new File("install.pph").exists())
			new File("install.pph").delete();
		try {
			new File("install.pph").createNewFile();
			cab = new DataOutputStream(new FileOutputStream(new File("install.pph")));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void writeFile(byte[] bytes, String name) {
		writeString(name);
		writeInt(bytes.length);
		writeBytes(bytes);
	}
	
	public void writeBytes(byte[] bytes) {
		try {
			cab.write(bytes, 0, bytes.length);
		} catch (IOException e) {
		}
	}
	
	public void writeInt(int i) {
		try {
			cab.writeInt(i);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void writeByte(byte b) {
		try {
			cab.writeByte(b);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void writeShort(short s) {
		try {
			cab.writeShort(s);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void writeString(String s)
	{
		short sLength = (short)s.length();
		writeShort(sLength);
		for(int i = 0; i < s.length(); i++)
		{
			byte b = (byte)s.substring(i, i + 1).charAt(0);
			writeByte(b);
		}
	}
	
	public void close() {
		try {
			cab.close();
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}

}
